package com.vectz.mgt;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.ait.lsp.msm.ui.decorator.util.Message;
import com.incors.plaf.alloy.AlloyLookAndFeel;
import com.incors.plaf.alloy.AlloyTheme;
import com.incors.plaf.alloy.themes.custom.CustomThemeFactory;
import com.vectz.mgt.util.Util;

/**
 * @author ai-t.vn
 * 
 */
public class MainGenerate {
	
	public static File fChkIns;
	public static FileChannel fChannel;
	public static FileLock fLock;
	
	public MainGenerate(){
		applyTheme();
		
		if (Util.isEmpty(System.getProperty("java.home"))) {
			Message.showErrorMsg(null, "JAVA_HOME");
		}
		// Check instance of MSM existed
		// Get a file channel for the file
		fChkIns = new File(System.getProperty("java.home")
				+ System.getProperty("file.separator") + "mmgs");
		if (fChkIns.exists()) {
			fChkIns.delete();
		}
		try {
			fChannel = new RandomAccessFile(fChkIns, "rw").getChannel();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		fChkIns.deleteOnExit();
		// lock file
		try {
			fLock = fChannel.tryLock();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (OverlappingFileLockException e) {
			// File is already locked in this thread or virtual machine
			JOptionPane.showMessageDialog(null, "OverlappingFileLockException",
					"Warning", JOptionPane.WARNING_MESSAGE);
			return;
		}
		if (fLock == null) {
			Message.showErrorMsg(null,"MMGS tool is already running. Do not allow start one more instance!");
			return;

		}
		
		new GenGUI();
	}
	private void applyTheme() {

		AlloyLookAndFeel.setProperty("alloy.licenseCode",
				"v#ej_technologies#uwbjzx#e6pck8");
		AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration",
				"true");

		try {
			// define colors
			Color contrast = new Color(112, 172, 105);
			Color standard = new Color(242, 242, 235);
			Color desktop = new Color(100, 103, 92);
			Color selection = new Color(171, 106, 66);
			Color rollover = new Color(254, 241, 47);
			Color highlight = new Color(251, 233, 219);

			// create custom theme
			AlloyTheme myTheme = CustomThemeFactory.createTheme(contrast,
					standard, desktop, selection, rollover, highlight);

			LookAndFeel alloyLnF = new AlloyLookAndFeel(myTheme);
			UIManager.setLookAndFeel(alloyLnF);
			AlloyLookAndFeel.updateAllUIs();
			// Register Enter, Space key press
			UIManager.getDefaults().put(
					"Button.focusInputMap",
					new UIDefaults.LazyInputMap(new Object[] { "ENTER",
							"pressed", "released ENTER", "released", "SPACE",
							"pressed", "released SPACE", "released" }));
			

		} catch (UnsupportedLookAndFeelException _ux) {
			_ux.printStackTrace();
		} catch (Exception _ex) {
			_ex.printStackTrace();
		}

		UIManager.getLookAndFeelDefaults().put("ClassLoader",
				getClass().getClassLoader());
	}
	
	public static void main(String args[]){
		
		new MainGenerate();
	}
}
